close all
NumOfStocks = 20;
NumOfBlocks = 5;
%[CovBlocks, Combinations, SampleSizes, Sigma, StockReturns]=GetCovBlocks(0);
load data_5_blocks.mat

norms = {'fro', 1, 2};
error = zeros(length(norms));
    
for i=1:length(norms),
    cvx_begin
        cvx_quiet(true); 
        variable Sigma_hat(NumOfStocks, NumOfStocks) symmetric;
    
        %Define objective function
        f = 0;
        s_max = 0;
        for q=1:NumOfBlocks
            %Add the norm of differences between block covariance and the
            %corresponding block in X.
            f = f + norm(Sigma_hat(Combinations{q}, Combinations{q})-CovBlocks{q},norms{i});
           
            s_max = max(s_max, max(max(CovBlocks{q})));
        end
        minimize (f)
    
        subject to
        Sigma_hat == semidefinite(NumOfStocks)
        Sigma_hat(:) <= s_max;
        Sigma_hat(:) >= -s_max;
    cvx_end
    
    Sigmas_hat{i} = Sigma_hat;
    error(i) = norm(Sigma_hat-Sigma, norms{i});
    fs(i) = f;
    figure; hist(Sigmas_hat{i}(:)-Sigma(:));
end